import { userInfo, getGemCount } from '@/gql/user';
import { getFleetInfo, getInvoiceInfo } from '@/gql/fleet';
import { getAmountRegionTotal, getAmountRegion } from '@/gql/warehouse';
import { getDriverInfo } from '@/gql/integral';
import { toPublish } from '@mqtt';
import { defineStore } from 'pinia';
import { getDriverID } from './driverID';
import { showLoading } from '@/utils/prompt';
export const useUserStore = defineStore('user', {
  state: () => ({
    customers_id: null as number,
    avatar: '',
    mobile: '',
    nickname: '',
    realname: '',
    role_text: '',
    money: '',
	cart_code:'',
    score: '',
    is_plug: 0,
    empower_money: '',
    fenbo_money: '',
    store_id: uni.getStorageSync('store_id') ? uni.getStorageSync('store_id') : 0,
    friendUserInfo: uni.getStorageSync('friendUserInfo') ? uni.getStorageSync('friendUserInfo') : {},
    getFleetAll: uni.getStorageSync('getFleetAll') ? uni.getStorageSync('getFleetAll') : {},
    invoiceAll: uni.getStorageSync('invoiceAll') ? uni.getStorageSync('invoiceAll') : {},
    integral: uni.getStorageSync('integral') ? uni.getStorageSync('integral') : 0,
    gem: uni.getStorageSync('gem') ? uni.getStorageSync('gem') : 0,
    amount: uni.getStorageSync('amount') ? uni.getStorageSync('amount') : 0,
    physical: []
  }),
  getters: {},
  actions: {
    getUserInfo(handler = () => { }) {
      const payload = {
        query: userInfo,
        variables: { driver_id: getDriverID() },
      };
      toPublish(
        'ql/driver/getDriverInfo',
        payload,
        (obj : any) => {
          const userInfo = obj.data;
          const { driver_info } = userInfo;
          if (!driver_info.nickname) {
            uni.reLaunch({
              url: '/pages/login/login',
            });
          }
          this.customers_id = driver_info.customers_id;
          this.avatar = driver_info.avatar;
          this.mobile = driver_info.mobile;
          this.nickname = driver_info.nickname;
          this.realname = driver_info.realname;
          this.role_text = driver_info.role_text;
          this.money = driver_info.money;
          this.score = driver_info.score;
          this.empower_money = driver_info.empower_money
          this.is_plug = driver_info.is_plug
          this.fenbo_money = driver_info.fenbo_money
          this.store_id = driver_info.store_id
		  this.cart_code=driver_info.cart_code
          uni.setStorageSync('store_id', driver_info.store_id)
          handler();
        },
      );
    },
    // 车友信息
    friendUser(num : string) {
      const payload = {
        query: userInfo,
        variables: { driver_id: Number(num) },
      };
      toPublish(
        'ql/driver/getDriverInfo',
        payload,
        (obj : any) => {
          const { driver_info } = obj.data;
          this.friendUserInfo = driver_info
          uni.setStorageSync('friendUserInfo', driver_info)
        },
      );
    },
    //车队信息
    fleetUser() {
      const payload = {
        query: getFleetInfo,
        variables: { driver_id: getDriverID() },
      };
      toPublish(
        'ql/driver/getFleetInfo',
        payload,
        (obj : any) => {
          const { getFleetInfo } = obj.data;
          this.getFleetAll = getFleetInfo
          uni.setStorageSync('getFleetAll', getFleetInfo)
        },
      );
    },
    //发票信息
    invoice(num : string) {
      showLoading()
      const payload = {
        query: getInvoiceInfo,
        variables: {
          id: Number(num),
        },
      };
      toPublish(
        'ql/driver/getInvoiceInfo',
        payload,
        (obj : any) => {
          const { getInvoiceInfo } = obj.data;
          this.invoiceAll = getInvoiceInfo
          uni.setStorageSync('invoiceAll', getInvoiceInfo)
        }
      );
    },
    // 积分信息
    integrate() {
      showLoading()
      const payload = {
        query: getDriverInfo,
        variables: {
          driver_id: getDriverID()
        },
      };
      toPublish(
        'ql/score/getDriverInfo',
        payload,
        (obj : any) => {
          const { getDriverInfo } = obj.data;
          this.integral = getDriverInfo.score
          uni.setStorageSync('integral', getDriverInfo.score)
        }
      );
    },
    // 宝石信息
    gemstone() {
      showLoading()
      const payload = {
        query: getGemCount,
        variables: {
          driver_id: getDriverID()
        },
      };
      toPublish(
        'ql/driver/getGemCount',
        payload,
        (obj : any) => {
          const { getGemCount } = obj.data;
          this.gem = getGemCount.gem
          uni.setStorageSync('gem', getGemCount.gem)
        }
      );
    },
    // 实物库存
    Physicalamounte(user : number) {
      showLoading()
      const payload = {
        query: getAmountRegionTotal,
        variables: {
          driver_id: user
        },
      };
      toPublish(
        'ql/driver/getAmountRegionTotal',
        payload,
        (obj : any) => {
          const { getAmountRegionTotal } = obj.data;
          this.amount = getAmountRegionTotal.amount
          uni.setStorageSync('amount', getAmountRegionTotal.amount)
        }
      );
    },
    //实物列表
    inventory(user : number) {
      showLoading()
      const payload = {
        query: getAmountRegion,
        variables: {
          driver_id: user
        },
      };
      toPublish(
        'ql/driver/getAmountRegion',
        payload,
        (obj : any) => {
          const { getAmountRegion } = obj.data;
          this.physical = getAmountRegion
        }
      );
    }
  },
});